import {
    rowPrint
  } from "@/api/cabinet/cabinetPrint/index"
const hanYinTarget = window.proxy || window
console.log('hanYinTarget', hanYinTarget)
export const HanYinPrint = {
    data () {
      const self = this;
      return {
        g_running: false,
        g_tasks: new Array,
        noSupportMessage: "Your browser cannot support WebSocket!",
        isconnecting: false,
        tagIp: '172.16.50.29'// 打印二维码ip： 172.16.50.29 打印条形码ip： 172.16.50.30
      }
    },
    methods: {
      //连接服务器
        connect() {
            console.log(this.isconnecting, hanYinTarget.hanYin_isconnected)
            if (this.isconnecting || hanYinTarget.hanYin_isconnected) return false;
            let ip = '127.0.0.1';
            var port = '9099';
            var timeout = 3000;
            var tag = this.tagIp
            // var tag = '10.182.54.42'
            var tag_port = '9100'
            let that = this
            console.log("connect section recive：",this.tagIp)
            var support = "MozWebSocket" in window ? 'MozWebSocket' : ("WebSocket" in window ? 'WebSocket' : null);

            if (support == null) {
                alert("* " + noSupportMessage + "<br/>");//提示错误信息
                return false;
            }

            console.log("* 服务器连接中 ......");

            // 创建一个新连接
            hanYinTarget.hanyinWs = new window[support]('ws://' + ip + ':' + port);
            
            //hanYinTarget.hanyinWs._create_default_https_context = hanYinTarget.hanyinWs._create_unverified_context

            hanYinTarget.hanyinWs.onerror = function (e) {
                alert("error " + e);
            }
            // hanYinTarget.hanyinWs.onclose = function (e){
            // 	alert("connect closed:" + e.code);
            // 	//hanYinTarget.hanyinWs.close();
            // }
            
            //连接建立之后
            hanYinTarget.hanyinWs.onopen = function () {
                console.log("连接已建立!");
                this.isconnecting = false;
                hanYinTarget.hanYin_isconnected = true;
                that.goon();
            };

            // 返回数据处理
            hanYinTarget.hanyinWs.onmessage = function (evt)
            {
                var obj = JSON.parse(evt.data);
                if (obj.code == 0)
                {
                    //状态
                    if (obj.fun != '' && obj.fun == 'ZPL_GetPrinterStatus') {
                        var str = "";
                        for (var i = 0; i < obj.data.length; i++) {
                            if (obj.data[i].name == "ZPL_STATE_NORMAL") {
                                if (obj.data[i].value == 1) {
                                    alert("正常");
                                    return;
                                }
                            }
                            if (obj.data[i].name == "ZPL_STATE_HIGHT_TEMP") {
                                if (obj.data[i].value == 1) {
                                    str += "高温;"
                                }
                            }
                            if (obj.data[i].name == "ZPL_STATE_STANDBY") {
                                if (obj.data[i].value == 1) {
                                    str += "待机;"
                                }
                            }
                            if (obj.data[i].name == "ZPL_STATE_PRINTING") {
                                if (obj.data[i].value == 1) {
                                    str += "打印中;"
                                }
                            }
                            if (obj.data[i].name == "ZPL_STATE_TOF_ERROR") {
                                if (obj.data[i].value == 1) {
                                    str += "标签定位异常;"
                                }
                            }
                            if (obj.data[i].name == "ZPL_STATE_LABEL_END") {
                                if (obj.data[i].value == 1) {
                                    str += "纸用尽;"
                                }
                            }
                            if (obj.data[i].name == "ZPL_STATE_RIBBON_END") {
                                if (obj.data[i].value == 1) {
                                    str += "碳带用尽;"
                                }
                            }
                            if (obj.data[i].name == "ZPL_STATE_LABEL_SEIZING") {
                                if (obj.data[i].value == 1) {
                                    str += "卡纸;"
                                }
                            }
                            if (obj.data[i].name == "STATE_LABEL_JUMPPING") {
                                if (obj.data[i].value == 1) {
                                    str += "跳标;"
                                }
                            }
                            if (obj.data[i].name == "ZPL_STATE_LABEL_CALIBRATING") {
                                if (obj.data[i].value == 1) {
                                    str += "标签学习中;"
                                }
                            }
                            if (obj.data[i].name == "ZPL_STATE_CUTTER_ERROR") {
                                if (obj.data[i].value == 1) {
                                    str += "切刀异常;"
                                }
                            }
                            if (obj.data[i].name == "ZPL_STATE_FORM_ERROR") {
                                if (obj.data[i].value == 1) {
                                    str += "标签格式异常;"
                                }
                            }
                            if (obj.data[i].name == "ZPL_STATE_MEMORY_WRITE_ERROR") {
                                if (obj.data[i].value == 1) {
                                    str += "记忆体写入异常;"
                                }
                            }
                            if (obj.data[i].name == "ZPL_STATE_ILLEGAL_COMMAND") {
                                if (obj.data[i].value == 1) {
                                    str += "非法指令;"
                                }
                            }
                            if (obj.data[i].name == "ZPL_STATE_COVER_OPEN") {
                                if (obj.data[i].value == 1) {
                                    str += "盒盖未到位;"
                                }
                            }
                            if (obj.data[i].name == "ZPL_STATE_RIBBON_NEAR_END") {
                                if (obj.data[i].value == 1) {
                                    str += "碳带将尽;"
                                }
                            }
                        }
                        alert(str);
                    }
                    else {
                        if (obj.fun != '') {
                            alert(obj.data);
                        }
                    }
                }

                that.goon();

                return obj;
            }

            this.isconnecting = true;

            return true;
        },

        //发送数据
        pushData(ip, port, data) {
            if (!hanYinTarget.hanYin_isconnected) {
                console.log("连接尚未建立，发送失败......");
                return;
            }

            hanYinTarget.hanyinWs.send(data);
        },

        //断开连接
        disconnectWebSocket() {
            if (hanYinTarget.hanyinWs) {
                hanYinTarget.hanyinWs.close();
                this.isconnecting = false
                hanYinTarget.hanYin_isconnected = false;
            }
        },
        goon() {
            //如果还存在任务，就继续发送
            console.log('存在任务，执行一条')
            if (this.g_tasks.length > 0) {
                var p = this.g_tasks[0];
                this.g_tasks.splice(0, 1);	
                p.print();
            }
            else {
                console.log('没有任务，断开链接')
                //断开连接
                this.disconnectWebSocket();
            }
        },

        print(printdata) {
            //配置打印参数
            var ip = '127.0.0.1';
            var port = '9099';
            var timeout = 3000;
            var tag = this.tagIp;
            // var tag = '10.182.54.42'
            var tag_port = '9100'

            var model = "XT330"
            var interfacePort = 'NET'
            console.log("print section recive：",this.tagIp)
            var printer = new hanYinTarget.ZPL_JSSDK.Printer(ip, model, timeout, port, interfacePort, tag, tag_port);
            printer.setPrintData(printdata);

            this.g_tasks.push(printer);

            this.connect();
        },

        printReport(id) {
            rowPrint(id).then(res=> {
              if(res.data.code == 0) {
                this.$message.success("打印操作执行成功！")
                this.refresh()
              }
            })
        },
    }
};
  
  